package reese.module_redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * ***********************************************************
 *
 * @类名 : reese.module_redis.类名
 * @DESCRIPTION : 这家伙太懒啥都没写
 * @AUTHOR :  一个风情万种的男子
 * @DATE : 2016/4/21
 * ***********************************************************
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-beans.xml")
public class RedisTest01 {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private ShardedJedisPool shardedJedisPool ;

    @Test
    public void basicOpTest() {
        Jedis jedis = jedisPool.getResource();
        jedis.set("reese.001.name", "reese");
        jedis.set("reese.001.sex", "男");


        String name = jedis.get("reese.001.name");
        String sex = jedis.get("reese.001.sex");

        assert name.equals("reese");
        assert sex.equals("男");

        jedis.del("reese.001.sex");

        Boolean aBoolean = jedis.exists("reese.001.sex");

        System.out.println("RedisTest01.basicOpTest :" + aBoolean);


    }

    /**
     * redis分片技术实现，配置估计有问题，绿条测试不通过
     * --已经知晓问题所在，需要配置redis主从架构，从而配置端口号
     * --参见:http://blog.csdn.net/a67474506/article/details/50435206
     */
    @Test
    public void redisShardTest(){

        ShardedJedis shardedJedis = shardedJedisPool.getResource();

        long begin = System.currentTimeMillis();

        for(int i=0;i<10000;i++){
            shardedJedis.set("name.key."+i,"reese.name."+i);
            shardedJedis.set("sex.key."+i,"reese.sex."+i);

            String name = shardedJedis.get("name.key."+i);
            String sex = shardedJedis.get("sex.key."+i);

            System.out.println("sex = " + sex);
            System.out.println("name = " + name);


        }
        long end = System.currentTimeMillis();

        for(Jedis myJedis : shardedJedis.getAllShards()){
            System.out.println("RedisTest01.redisShardTest:"+myJedis.getClient().getHost()+":"+myJedis.getClient().getPort());
            System.out.println("redis shard size:"+myJedis.dbSize());
        }

        System.out.println("total time: " + (end-begin)/1000);
        shardedJedis.close();

    }

}
